package core

import (
	"bytes"
	"crypto/sha256"
	"crypto/sm3"
	"encoding/binary"
	"errors"
)

func CalculateHash(data []byte, hashmethod uint64) ([]byte, error) {
	switch hashmethod {
	case SM3:
		h := sm3.New()
		if _, err := h.Write(data); err != nil {
			return nil, err
		}
		return h.Sum(nil), nil
	case SHA256:
		h := sha256.New()
		if _, err := h.Write(data); err != nil {
			return nil, err
		}
		return h.Sum(nil), nil
	default:
		return nil, errors.New("Undefault Hash Method")
	}
}

//整形转换成字节
func IntToBytes(n int) []byte {
	x := int32(n)
	bytesBuffer := bytes.NewBuffer([]byte{})
	binary.Write(bytesBuffer, binary.BigEndian, x)
	return bytesBuffer.Bytes()
}

//字节转换成整形
func BytesToInt(b []byte) int {
	bytesBuffer := bytes.NewBuffer(b)
	var x int32
	binary.Read(bytesBuffer, binary.BigEndian, &x)
	return int(x)
}
